import com.fxd.dao.IUserDao;
import com.fxd.dao.impl.userdaoimpl;
import com.fxd.doman.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @auther fuxudong
 * @date 2020/12/17 - 17:11
 */
public class crudmabatistest {
    private InputStream in;
    private IUserDao userDao;

    @Before
    public void init() throws Exception {
        //1，读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2, 创建SQLsessionfactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3, 使用工厂生产SQLsession对象
        userDao= new userdaoimpl(factory);
    }

    @After
    public void destroy() throws Exception {
        in.close();

    }


    @Test
    public void findalltest() {

        //5, 使用代理对象执行方法
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void testsave() {
        //获取保存之后的ID
        User user = new User();
        user.setUsername("stone");
        user.setAddress("ch");
        user.setSex("g");
        user.setBirthday(new Date());
        System.out.println(user);
        userDao.saveUser(user);
        System.out.println(user);
    }
    @Test
    public void testupdate() {
        User user = new User();
        user.setId(52);
        user.setUsername("stoi");
        user.setAddress("ch");
        user.setSex("g");
        user.setBirthday(new Date());
        userDao.updateUser(user);
    }
    @Test
    public void testdelete(){
        userDao.deleteUser(52);
    }

    @Test
    public void testfindone(){
        User user = userDao.findById(48);
        System.out.println(user);
    }
    @Test
    public void testfindbyname(){
       List<User> users = userDao.findByName("%王%");
       for(User user :users){
           System.out.println(user);
       }
    }
    @Test
    public void testfindtotol(){
        Integer sum = userDao.findtotol();
        System.out.println("总数是"+sum);

    }
//    @Test
//    public void testfindbyvo(){
//        QueryVo queryVo = new QueryVo();
//        User user = new User();
//        user.setUsername("%王%");
//        queryVo.setUser(user);
//        List<User> users = userDao.findUserByVo(queryVo);
//        for(User u :users){
//            System.out.println(u);
//        }
//    }


}
